
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>HandlerInterceptor和Filter的区别是什么 - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>HandlerInterceptor和Filter的区别是什么 - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">在Spring MVC中，`HandlerInterceptor` 和 `Filter` 在作用范围上有什么本质区别？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`HandlerInterceptor` 是 Spring MVC 特有的，仅作用于 Spring MVC 请求处理的生命周期内。而 `Filter` 是 Servlet 规范的一部分，作用于整个 Servlet 容器，适用于所有进入容器的请求和响应。</div>
          </div>
          <div class="card-source">来源: 1. `HandlerInterceptor` 和 `Filter` 的区别</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">`HandlerInterceptor` 接口提供了哪三个核心方法，它们分别在请求处理的哪个阶段被调用？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">1. `preHandle`: 在请求到达控制器之前执行。<br>2. `postHandle`: 在控制器方法执行之后、视图渲染之前执行。<br>3. `afterCompletion`: 在请求处理完毕、视图渲染之后执行。</div>
          </div>
          <div class="card-source">来源: 1.1 `HandlerInterceptor`</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">如果 `HandlerInterceptor` 的 `preHandle` 方法返回 `false`，会对请求处理流程产生什么影响？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">如果 `preHandle` 方法返回 `false`，整个请求将被终止，后续的拦截器以及控制器方法都不会被执行。</div>
          </div>
          <div class="card-source">来源: 1.1 `HandlerInterceptor`</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">如何在Spring配置类中注册一个自定义的 `HandlerInterceptor`？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">通过实现 `WebMvcConfigurer` 接口，并重写 `addInterceptors` 方法。在该方法中，使用 `InterceptorRegistry` 对象来添加自定义的拦截器，并可以配置其拦截的URL模式。</div>
          </div>
          <div class="card-source">来源: 2.2 注册自定义拦截器</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">`Filter` 接口的核心处理方法是什么？在该方法中，如何将请求传递给下一个过滤器或目标资源？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">核心处理方法是 `doFilter(ServletRequest request, ServletResponse response, FilterChain chain)`。通过调用 `chain.doFilter` 方法可以将请求传递到过滤器链中的下一个组件。</div>
          </div>
          <div class="card-source">来源: 1.2 `Filter`</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">除了传统的 `web.xml` 配置，还有哪些方式可以注册一个自定义的 `Filter`？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">可以通过使用 `@WebFilter` 注解，或者在Java配置类中创建一个 `FilterRegistrationBean` 类型的Bean来注册自定义的 `Filter`。</div>
          </div>
          <div class="card-source">来源: 2.4 注册自定义过滤器</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
